package org.demo.warehouse.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zgc
 * @since 2019/10/22
 */
@Slf4j
public class WebUtil {
    public enum WebRequestType {
        ajax, no_ajax
    }

    public static WebRequestType requestType(HttpServletRequest request) {
        String header = request.getHeader("X-Requested-With");
        WebRequestType type = "XMLHttpRequest".equalsIgnoreCase(header) ? WebRequestType.ajax : WebRequestType.no_ajax;
        return type;
    }

    public static String getRequestIp(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = null;
        try {
            ip = request.getHeader("x-forwarded-for");
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } catch (Exception e) {
            log.error("WebUtil ERROR ", e);
        }
        return ip;
    }

    public static String getServerIp() {
        String serverIP = "";
        Enumeration<NetworkInterface> netInterfaces = null;
        InetAddress ip = null;
        try {
            netInterfaces = NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = (NetworkInterface) netInterfaces.nextElement();
                Enumeration<InetAddress> ips = networkInterface.getInetAddresses();
                while (ips.hasMoreElements()) {
                    ip = ips.nextElement();
                    // 1.获取符合条件的ip
                    if (ip != null && ip instanceof Inet4Address && ip.isSiteLocalAddress() &&
                            !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(".") != -1) {
                        serverIP = ip.getHostAddress();
                    } else {
                        ip = null;
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return serverIP;
    }

    public static Map<String, HttpSession> sessions = new HashMap<String, HttpSession>();

    public static Map<String, HttpSession> activeSessions(){
        if(sessions ==null){
            sessions = new HashMap<String, HttpSession>();
        }
        return sessions;
    }
}
